package com.info.threads;

import java.util.Date;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.info.server.Event;

/**
 * @author HAHA
 * 
 */
public class TerPoolThread extends Thread {
	private static Logger logger=Logger.getLogger(TerPoolThread.class);
	private int maxThread;
	public static Vector vector;
	private static String str;
	public static Vector msgList = new Vector();

	public void setMaxThread(int threadCount) {
		this.maxThread = threadCount;
		setName("ThreadPoolManager");
	}

	public TerPoolThread(int threadCount) {
		setMaxThread(threadCount);
		logger.info("SHW#启动线程池...");
		Date date = new Date();
		vector = new Vector();

		for (int i = 1; i <= this.maxThread; i++) {
			TerThread thread = new TerThread(i);
			vector.addElement(thread);
			thread.start();
		}
		start();
	}

	public void run() {
		Event argument = null;
		do{
			try {
				for(int i = 0; i < vector.size(); i++){
				TerThread currentThread = (TerThread) vector.elementAt(i);
				if (!currentThread.isRunning() && !msgList.isEmpty()) {
					argument = (Event) msgList.remove(0);
					currentThread.setArgument(argument);
					currentThread.setRunning(true);
				}}
				try {
					Thread.sleep(1L);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			} catch (Exception e) {
				logger.info("SHW#有子线程异常！");
			}}while (true);
	}

	public synchronized void process(Object argument) {
		try {
			msgList.add(argument);
		} catch (Exception e) {
			logger.info("SHW#添加消息错误！");
			e.printStackTrace();
		}
	}
}